package jjn.round1;

import commons.TreeNode;

/**
 * @author Jjn
 * @since 2022/9/25 20:12
 */
public class LeetCode543_DiameterOfBinaryTree {

    private int maxDepth;

    public int diameterOfBinaryTree(TreeNode root) {
        if (root == null) {
            return 0;
        }
        calcHeight(root);
        return maxDepth;
    }

    private int calcHeight(TreeNode node) {
        if (node == null) {
            return 0;
        }
        int left = calcHeight(node.left);
        int right = calcHeight(node.right);
        int max = left + right;
        maxDepth = Math.max(max, maxDepth);
        return Math.max(left, right) + 1;
    }

}
